package com.huikaka.controller;

import com.huikaka.entity.Result;
import com.huikaka.entity.User;
import com.huikaka.service.UserService;
import com.huikaka.util.UniqueInviteCodeUtile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    获取用户的openid
//    @RequestMapping("/getOpenId")
//    public String getUserInfo(@RequestParam(name = "code") String code) throws Exception {
//        System.out.println("code" + code);
//        String url = "https://api.weixin.qq.com/sns/jscode2session";
//        url += "?appid=wxd5cfbc958ac33915";//自己的appid
//        url += "&secret=xxxxxxxxxxxxxxxxxxx";//自己的appSecret
//        url += "&js_code=" + code;
//        url += "&grant_type=authorization_code";
////        url += "&connect_redirect=1";
//        String res = null;
//        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//        // DefaultHttpClient();
//        HttpGet httpget = new HttpGet(url);    //GET方式
//        CloseableHttpResponse response = null;
//        // 配置信息
//        RequestConfig requestConfig = RequestConfig.custom()          // 设置连接超时时间(单位毫秒)
//                .setConnectTimeout(5000)                    // 设置请求超时时间(单位毫秒)
//                .setConnectionRequestTimeout(5000)             // socket读写超时时间(单位毫秒)
//                .setSocketTimeout(5000)                    // 设置是否允许重定向(默认为true)
//                .setRedirectsEnabled(false).build();           // 将上面的配置信息 运用到这个Get请求里
//        httpget.setConfig(requestConfig);                         // 由客户端执行(发送)Get请求
//        response = httpClient.execute(httpget);                   // 从响应模型中获取响应实体
//        HttpEntity responseEntity = response.getEntity();
//        System.out.println("响应状态为:" + response.getStatusLine());
//        if (responseEntity != null) {
//            res = EntityUtils.toString(responseEntity);
//            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
//            System.out.println("响应内容为:" + res);
//        }
//        // 释放资源
//        if (httpClient != null) {
//            httpClient.close();
//        }
//        if (response != null) {
//            response.close();
//        }
//        JSONObject jo = JSON.parseObject(res);
//        String openid = jo.getString("openid");
//        System.out.println("openid" + openid);
//        return openid;
//    }


//    添加用户
    @RequestMapping(value = "/addUser",method = RequestMethod.GET)
    public Result addUser(@RequestParam String openId,@RequestParam String nickName,
                          @RequestParam String headerUrl,@RequestParam String role){
        Result result = new Result();
        try{
            UniqueInviteCodeUtile uniqueInviteCodeUtile = new UniqueInviteCodeUtile();
            String code = null;
            boolean flag = true;
            while (flag){
                code = uniqueInviteCodeUtile.getOneCode();
                User user = userService.findUserByInviteCode(code);
                if(user==null){
                    flag = false;
                }
            }

            Integer integer = userService.addUser(openId,code,nickName,headerUrl,role);
            if(integer==1){
                result.setCode(0);
                result.setMessage("成功");
            }
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("失败");
            System.out.println(e);
        }
        return result;
    }

//    通过微信openid查找用户
    @RequestMapping("/findUserByOpenId")
    public Result findUserByOpenId(@RequestParam String openId){
        Result result = new Result();
        try{
            User user = userService.findUserByOpenId(openId);
            if(user!=null){
                result.setCode(0);
                result.setMessage("成功");
                result.setData(user);
                return result;
            }
            result.setCode(1);
            result.setMessage("失败");
        }catch (Exception e){

        }
        return result;
    }

//    通过邀请码查找用户
    @RequestMapping("/findUserByInviteCode")
    public Result findUserByInviteCode(@RequestParam String inviteCode){
        Result result = new Result();
        try{
            User user = userService.findUserByInviteCode(inviteCode);
            System.out.println(user);
            if(user!=null){
                result.setCode(0);
                result.setMessage("成功");
                result.setData(user);
                return result;
            }
            result.setCode(1);
            result.setMessage("失败");
        }catch (Exception e){

        }
        return result;
    }
}
